package com.eazy.lkv2.web.controller.hotel;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.eazy.lkv2.web.controller.BaseController;


/**
 * 2016-7-27 上午 10:00
 * @author jzx
 * 
 * 有酒店图片上传，那么自然就有房间图片上传啦。
 */
@Controller
@RequestMapping("/room_photo")
public class RoomPhotoUploadController extends BaseController {
	
	/**
	 * 房间图片上传
	 */
	@RequestMapping(value = "roomPhotoUpload" , method = { RequestMethod.POST, RequestMethod.GET })
	public void roomPhotoUpload(HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException, URISyntaxException {
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());  
        if(multipartResolver.isMultipart(request)){  
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;  
            Iterator<String> iter = multiRequest.getFileNames();  
            while(iter.hasNext()){  
                MultipartFile file = multiRequest.getFile(iter.next());  
                if(file != null){  
                    String myFileName = file.getOriginalFilename();  
                    if(myFileName.trim() !=""){  
                        String fileName =  file.getOriginalFilename();  
                        fileName = fileName.substring(0, fileName.lastIndexOf(".")) + ".jpg";
                        // 改写成自己的磁盘路径
                        String windows = "E:\\room\\" + fileName;
                        String linux = "/usr/local/room/" + fileName;
                        file.transferTo(new File(linux));
                    }  
                }  
            }  
              
        }  
	}
}
